package org.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.example.domain.ResponseResult;
import org.example.domain.dto.UserListDto;
import org.example.domain.entity.User;
import org.example.domain.vo.PageVo;
import org.example.domain.vo.UserInfoVo;
import org.example.enums.AppHttpCodeEnum;
import org.example.exception.SystemException;
import org.example.mapper.UserMapper;
import org.example.service.UserService;
import org.example.utils.BeanCopyUtils;
import org.example.utils.SecurityUtils;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

@Service
@RequiredArgsConstructor
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements UserService {

    private final UserMapper userMapper;

    private final PasswordEncoder passwordEncoder;


    @Override
    public ResponseResult userInfo() {

        Integer userId =  SecurityUtils.getUserId();

        User user = userMapper.SelectByUserId(userId);

        UserInfoVo userInfoVo = BeanCopyUtils.copyBean(user,UserInfoVo.class);

        return ResponseResult.okResult(userInfoVo);

    }

    @Override
    @Transactional
    public ResponseResult updateUser(User user) {

        boolean flag = userMapper.updateUser(user);
        if(flag){
            return ResponseResult.okResult();
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.UPDATE_ERROR);
    }

    @Override
    @Transactional
    public ResponseResult register(User user) {
        if( StringUtils.isBlank(user.getUserName())){
            throw new SystemException(AppHttpCodeEnum.USERNAME_NOT_ERROR);
        }

        if(StringUtils.isBlank(user.getEmail())){
            throw new SystemException(AppHttpCodeEnum.EMAIL_NOT_ERROR);
        }

        if(StringUtils.isBlank(user.getPassword())){
            throw new SystemException(AppHttpCodeEnum.PASSWORD_NOT_ERROR);
        }

        if(StringUtils.isBlank(user.getNickName())){
            throw new SystemException(AppHttpCodeEnum.NICKNAME_NOT_ERROR);
        }
        if(StringUtils.isBlank(user.getConfirmPassword())){
            throw new SystemException(AppHttpCodeEnum.CONFIRM_PASSWORD_NOT_ERROR);
        }
        System.out.println(user.getConfirmPassword().equals(user.getPassword()));
        if(!user.getConfirmPassword().equals(user.getPassword())){
            throw new SystemException(AppHttpCodeEnum.DIFFERENT_ERROR);
        }
        User user1 = userMapper.SelectByUserName(user.getUserName());
        if(!Objects.isNull(user1)){
            throw new SystemException(AppHttpCodeEnum.USERNAME_EXIST);
        }
        user1 = userMapper.SelectByEmail(user.getEmail());
        if(!Objects.isNull(user1)){
            throw new SystemException(AppHttpCodeEnum.EMAIL_EXIST);
        }
        user1 = userMapper.SelectByNickName(user.getNickName());
        if(!Objects.isNull(user1)){
            throw new SystemException(AppHttpCodeEnum.NICKNAME_EXIST);
        }

        String EncoderPassword = passwordEncoder.encode(user.getPassword());
        user.setPassword(EncoderPassword);
        userMapper.save(user);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult userList(UserListDto userListDto) {

        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(userListDto.getUserName()),User::getUserName,userListDto.getUserName());

        Page<User> page = new Page<>();
        page.setCurrent(userListDto.getPageNum());
        page.setSize(userListDto.getPageSize());
        page(page,queryWrapper);

        PageVo pageVo = new PageVo(page.getRecords(),page.getTotal());

        return ResponseResult.okResult(pageVo);
    }

    @Override
    public boolean changeUserStatus(User user) {
        return userMapper.changeUserStatus(user);
    }


}
